package com.study01;

import java.io.*;

public class Demo03 {
    public static void main(String[] args) {
        //拷贝一张图片，从一个目录到另外一个目录下(PS:是拷贝是不是移动)
        copyImage("E:/desk/desk-1.jpg","d:/desk-1.jpg");
    }
    public static void copyImage(String srcPath, String destPath) {
        File file = new File(srcPath);

        if(file.exists()){
            InputStream is = null;
            OutputStream os = null;
            try {
                is = new FileInputStream(file);
                os = new FileOutputStream(destPath);
                byte[] buffer = new byte[1024];
                int count = -1;
                while((count = is.read(buffer)) != -1){
                    //一边读，一边写。
                    os.write(buffer,0,count);
                }
                System.out.println("文件拷贝完成");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(is != null){
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                };
                if(os != null){
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }else{
            System.out.println("文件不存在，请检查目录");
        }
    }
}
